package com.gcb.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

import com.gcb.util.FileUtil;

public class TestCopy {

	public void copy(String srcFile, String descFile){
		try {
			FileInputStream fis = new FileInputStream(srcFile);
			FileOutputStream fos = new FileOutputStream(descFile);
			long st = System.currentTimeMillis();
			int i = fis.read();
			while(i != -1){
				fos.write(i);
				i = fis.read();
			}
			System.out.println("总共花费时间：" + (System.currentTimeMillis() - st) + "毫秒！");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void copy2(String srcFile, String descFile){
		try {
			FileInputStream fis = new FileInputStream(srcFile);
			FileOutputStream fos = new FileOutputStream(descFile);
			FileChannel fic = fis.getChannel();
			FileChannel foc = fos.getChannel();
			ByteBuffer dst = ByteBuffer.allocate(10240000);
			
			long st = System.currentTimeMillis();
			fic.read(dst);
			dst.flip();
			foc.write(dst);
			System.out.println(dst.capacity());
			System.out.println("总共花费时间：" + (System.currentTimeMillis() - st) + "毫秒！");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public String creFile(String fileName){
		String path = FileUtil.getCurJaPath(getClass());
		File f = new File(path , fileName);
		if(!f.exists()){
			try {
				f.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return f.getPath();
	}
	
	
	public static void main(String[] args) {
		TestCopy tc = new TestCopy();
		tc.copy2("F:\\My DBank\\批量下载\\Disk Tool.rar", "E:/gcb/workspace_ch/myweb/src/com/gcb/io/2.rar");
		//System.out.println(tc.creFile("ttt.t"));
	}
}
